<template>
  <div class="basic-configuration-container">
    <ContentWrap class="config-wrapper">
      <!-- 配置头部 -->
      <div class="config-header">
        <div class="header-left">
          <div class="header-icon">
            <Icon icon="ep:info-filled" />
          </div>
          <div class="header-content">
            <h4 class="config-title">基本信息</h4>
            <p class="config-subtitle">配置租户的基本信息和参数</p>
          </div>
        </div>
        <el-button type="primary" @click="preservationConfig" class="save-btn">
          <Icon icon="ep:check" class="btn-icon" />
          保存
        </el-button>
      </div>

      <!-- 表单内容 -->
      <div class="form-container">
        <el-form
          ref="formRef"
          v-loading="loading"
          :model="formData"
          :rules="formRules"
          label-width="120px"
          class="enhanced-form"
        >
          <!-- 基本信息 -->
          <div class="form-section">
            <div class="section-title">
              <Icon icon="ep:user" class="section-icon" />
              <span>租户信息</span>
            </div>

            <el-form-item label="租户名" prop="name" class="form-item">
              <el-input v-model="formData.name" placeholder="请输入租户名" class="enhanced-input">
                <template #prefix>
                  <Icon icon="ep:office-building" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="租户套餐" prop="packageId" class="form-item">
              <el-select
                v-model="formData.packageId"
                clearable
                placeholder="请选择租户套餐"
                class="enhanced-select"
              >
                <template #prefix>
                  <Icon icon="ep:box" />
                </template>
                <el-option
                  v-for="item in packageList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="联系人" prop="contactName" class="form-item">
              <el-input
                v-model="formData.contactName"
                placeholder="请输入联系人"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:user" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="联系手机" prop="contactMobile" class="form-item">
              <el-input
                v-model="formData.contactMobile"
                placeholder="请输入联系手机"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:phone" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="过期时间" prop="expireTime" class="form-item">
              <el-date-picker
                v-model="formData.expireTime"
                clearable
                placeholder="请选择过期时间"
                type="date"
                value-format="x"
                class="enhanced-date-picker"
              />
            </el-form-item>

            <el-form-item label="绑定域名" prop="website" class="form-item">
              <el-input
                v-model="formData.website"
                placeholder="请输入绑定域名"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:link" />
                </template>
              </el-input>
            </el-form-item>
          </div>

          <!-- 财务信息 -->
          <div class="form-section">
            <div class="section-title">
              <Icon icon="ep:money" class="section-icon" />
              <span>财务信息</span>
            </div>

            <el-form-item label="余额" prop="wallet" class="form-item">
              <el-input
                v-model.trim="formData.wallet"
                type="number"
                placeholder="请输入余额"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:wallet" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="话费单价" prop="callCostPrice" class="form-item">
              <el-input
                v-model.trim="formData.callCostPrice"
                placeholder="话费单价"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:money" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="话费销售价" prop="callSalePrice" class="form-item">
              <el-input
                type="number"
                v-model="formData.callSalePrice"
                placeholder="设置该企业的话费销售价"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:sell" />
                </template>
              </el-input>
            </el-form-item>
          </div>

          <!-- 通信配置 -->
          <div class="form-section">
            <div class="section-title">
              <Icon icon="ep:phone" class="section-icon" />
              <span>通信配置</span>
            </div>

            <el-form-item label="绑定外呼套餐" prop="channelIdList" class="form-item">
              <el-select v-model="formData.channelIdList" multiple class="enhanced-select">
                <template #prefix>
                  <Icon icon="ep:headset" />
                </template>
                <el-option
                  v-for="item in callChannelList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="绑定外呼号码" prop="numberIdList" class="form-item">
              <el-select
                v-model="formData.numberIdList"
                filterable
                multiple
                class="enhanced-select"
              >
                <template #prefix>
                  <Icon icon="ep:phone" />
                </template>
                <el-option
                  v-for="item in callNumberList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.displayNumber"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="SIP主叫分组" class="form-item sip-group-item">
              <div class="sip-group-container">
                <el-row :gutter="10" class="w-100%">
                  <el-col :span="6">
                    <el-select
                      class="enhanced-select w-100%"
                      clearable
                      v-model="formData.cidGroup"
                      placeholder="请选择SIP主叫分组"
                    >
                      <template #prefix>
                        <Icon icon="ep:connection" />
                      </template>
                      <el-option
                        v-for="item in cidGroupOtions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="18">
                    <div class="sip-info-container">
                      <el-descriptions :column="1" border class="enhanced-descriptions">
                        <el-descriptions-item
                          :label="item.name"
                          v-for="(item, index) in cidGroupItem"
                          :key="index"
                          class="desc-item"
                        >
                          <div class="desc-value">
                            <Icon icon="ep:info-filled" class="value-icon" />
                            <span class="value-text">{{ item.value }}</span>
                          </div>
                        </el-descriptions-item>
                      </el-descriptions>
                    </div>
                  </el-col>
                </el-row>
              </div>
            </el-form-item>
          </div>

          <!-- 状态设置 -->
          <div class="form-section">
            <div class="section-title">
              <Icon icon="ep:switch" class="section-icon" />
              <span>状态设置</span>
            </div>

            <el-form-item label="租户状态" prop="status" class="form-item">
              <el-radio-group v-model="formData.status" class="enhanced-radio-group">
                <el-radio
                  v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                  :key="dict.value"
                  :label="dict.value"
                  class="enhanced-radio"
                >
                  <div class="radio-content">
                    <Icon :icon="dict.value === 0 ? 'ep:check' : 'ep:close'" class="radio-icon" />
                    <span>{{ dict.label }}</span>
                  </div>
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </ContentWrap>
  </div>
</template>

<script setup>
import { Tickets } from '@element-plus/icons-vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { CommonStatusEnum } from '@/utils/constants'
import * as TenantPackageApi from '@/api/system/tenantPackage'
import * as CallChannelApi from '@/api/call/channel'
import * as TenantApi from '@/api/system/tenant'
import { getCmOutnumberSimpleList } from '@/api/call/cmoutnumber'

const formRef = ref(null)
const loading = ref(false)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const formData = ref({
  id: undefined,
  name: undefined,
  packageId: undefined,
  contactName: undefined,
  contactMobile: undefined,
  expireTime: undefined,
  website: undefined,
  wallet: 0,
  status: CommonStatusEnum.ENABLE,
  cidGroup: null,
  callCostPrice: 0.13,
  callSalePrice: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '租户名不能为空', trigger: 'blur' }],
  packageId: [{ required: true, message: '租户套餐不能为空', trigger: 'blur' }],
  contactName: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '租户状态不能为空', trigger: 'blur' }],
  expireTime: [{ required: true, message: '过期时间不能为空', trigger: 'blur' }],
  website: [{ required: true, message: '绑定域名不能为空', trigger: 'blur' }],
  username: [{ required: true, message: '登录账号不能为空', trigger: 'blur' }],
  password: [{ required: true, message: '登录密码不能为空', trigger: 'blur' }],
  callCostPrice: [{ required: true, message: '话费单价不能为空', trigger: 'blur' }],
  callSalePrice: [{ required: true, message: '请设置该企业的话费销售价', trigger: 'blur' }]
})
const callChannelList = ref([])
const callNumberList = ref([])
const cidGroupOtions = ref([])
const packageList = ref([])
const getMap = async () => {
  packageList.value = await TenantPackageApi.getTenantPackageList()
  callChannelList.value = await CallChannelApi.getChannelList()
  callNumberList.value = await getCmOutnumberSimpleList()
  if (formData.value.cidGroup) setCidItem(formData.value.cidGroup)
}
const getlist = async (id) => {
  formData.value = await TenantApi.getTenant(id)
  if (formData.value.cidGroup) setCidItem(formData.value.cidGroup)
}
const props = defineProps({
  configureId: String
})
/*保存*/
const preservationConfig = async () => {
  await TenantApi.updateTenant(formData.value)
  message.success(t('common.updateSuccess'))
}
watch(
  () => props.configureId,
  (value) => {
    if (value) {
      getlist(value)
    }
  },
  { immediate: true, deep: true }
)
const cidGroupItem = ref(null)

watch(
  () => formData.value.cidGroup,
  (val) => {
    if (val) {
      setCidItem(val)
    } else {
      cidGroupItem.value = null
    }
  },
  { immediate: true, deep: true }
)
const setCidItem = (val) => {
  const obj = cidGroupOtions.value.find((item) => item.id == val)
  if (obj)
    cidGroupItem.value = [
      {
        name: 'cid',
        value: obj.cid
      },
      {
        name: 'did',
        value: obj.did
      },
      {
        name: 'channel',
        value: obj.channel
      }
    ]
}
/*sip分组*/
const getCidGroupList = async () => {
  try {
    cidGroupOtions.value = await TenantApi.getCidGroup()
  } catch {
    cidGroupOtions.value = []
  }
}
onMounted(async () => {
  await getCidGroupList()
  getMap()
})
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.basic-configuration-container {
  padding: 0;
}

.config-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

/* 配置头部 */
.config-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 16px;
  }
}

.header-left {
  display: flex;
  align-items: center;
  flex: 1;

  @media (max-width: 768px) {
    justify-content: center;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 10px;
  }
}

.config-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.config-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.save-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  color: white;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    background: white;
    color: $primary-color;
    border-color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(255, 255, 255, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }

    @media (max-width: 768px) {
      margin-bottom: 24px;
    }
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .form-item {
    margin-bottom: 20px;

    @media (max-width: 768px) {
      margin-bottom: 16px;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 输入框样式 */
.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 选择器样式 */
.enhanced-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focused {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }

  :deep(.el-select__tags) {
    flex-wrap: wrap;
    gap: 4px;
  }

  :deep(.el-tag) {
    background: $primary-light;
    color: $primary-color;
    border: 1px solid $primary-border;
    border-radius: 4px;
    font-size: 12px;
    padding: 2px 8px;
    margin: 2px;

    .el-tag__close {
      color: $primary-color;

      &:hover {
        background: $primary-color;
        color: white;
      }
    }
  }
}

/* 日期选择器样式 */
.enhanced-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 12px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* SIP分组容器 */
.sip-group-item {
  .sip-group-container {
    .sip-info-container {
      .enhanced-descriptions {
        :deep(.el-descriptions__table) {
          border-radius: $border-radius-small;
          overflow: hidden;
          box-shadow: $shadow-base;
        }

        :deep(.el-descriptions__cell) {
          padding: 12px 16px;
          border-color: $border-light;

          &.is-bordered-label {
            background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
            color: $text-primary;
            font-weight: 600;
            font-size: 13px;
            width: 80px !important;
          }

          &.is-bordered-content {
            background: white;
          }
        }
      }

      .desc-value {
        display: flex;
        align-items: center;
        gap: 6px;
      }

      .value-icon {
        color: $primary-color;
        font-size: 14px;
      }

      .value-text {
        color: $text-primary;
        font-weight: 500;
      }
    }

    // H5适配：SIP分组垂直布局
    @media (max-width: 768px) {
      :deep(.el-row) {
        flex-direction: column;
        gap: 12px;
      }

      :deep(.el-col) {
        width: 100% !important;
      }
    }
  }
}

/* 单选组样式 */
.enhanced-radio-group {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }
}

.enhanced-radio {
  :deep(.el-radio__input) {
    .el-radio__inner {
      border-color: $border-color;
      transition: all 0.3s ease;

      &:hover {
        border-color: $primary-color;
      }
    }

    &.is-checked {
      .el-radio__inner {
        background: $primary-color;
        border-color: $primary-color;

        &::after {
          background: white;
        }
      }
    }
  }

  :deep(.el-radio__label) {
    padding-left: 8px;
  }

  .radio-content {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .radio-icon {
    font-size: 14px;

    &.ep-check {
      color: $success-color;
    }

    &.ep-close {
      color: $danger-color;
    }
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #d1d5db;
      }
    }
  }

  .enhanced-input {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .enhanced-select {
    :deep(.el-select__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-select__placeholder {
        color: #9ca3af;
      }
    }

    :deep(.el-tag) {
      background: rgba(96, 165, 250, 0.1);
      color: #60a5fa;
      border-color: rgba(96, 165, 250, 0.3);
    }
  }

  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .sip-info-container {
    .enhanced-descriptions {
      :deep(.el-descriptions__cell) {
        border-color: #4b5563;

        &.is-bordered-label {
          background: #4b5563;
          color: #f9fafb;
        }

        &.is-bordered-content {
          background: #374151;
        }
      }
    }

    .value-text {
      color: #f9fafb;
    }
  }

  .enhanced-radio {
    :deep(.el-radio__label) {
      color: #d1d5db;
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .config-header {
    padding: 12px 16px;
  }

  .form-container {
    padding: 12px;
  }

  .enhanced-form {
    .form-section {
      margin-bottom: 20px;
    }

    .section-title {
      font-size: 13px;
      margin-bottom: 12px;
      padding-bottom: 6px;
    }

    .form-item {
      margin-bottom: 12px;

      :deep(.el-form-item__label) {
        font-size: 13px;
      }
    }
  }
}
</style>
